package com.profile.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.profile.domain.Profile;
import com.profile.service.ProfileService;
import com.profile.service.SkillService;
import com.profile.service.TittleService;
import com.profile.validation.ProfileValidation;

@Controller
@RequestMapping("/profileForm.html")
@SessionAttributes("profile")
public class ProfileFormController {

	@Autowired
	private ProfileService profileService;
	
	@Autowired
	private ProfileValidation profileValidation;
	
	@Autowired
	private SkillService skillService ;
	
	@Autowired
	private TittleService tittleService;

	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {

		dataBinder.setDisallowedFields(new String[] { "ID" });
		dataBinder.setRequiredFields(new String[] { "firstName", "lastName",
				"DOB","gender", "tittle" });
		dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(
				false));

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(false);
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping(params = "save", method = RequestMethod.POST)
	public String Save(Profile profile , BindingResult result, SessionStatus status, ModelMap model) {
		profileValidation.validate(profile, result);
		if (result.hasErrors()) {
			model.addAttribute("listSkills", skillService.GetAllSkill());
			model.addAttribute("listTittles",tittleService.GetAllTittles());
			return "profileForm";
		}else{
			profileService.saveProfile(profile);
			status.setComplete();
			return "redirect:home.html";
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	public String setUpForm(
			@RequestParam(value = "id", required = false) Integer profileId , ModelMap model) {
		Profile profile = null ;
		if (profileId == null) {
			profile = new Profile();
		} else {
			profile =profileService.getProfileById(profileId);
		}
		model.addAttribute("profile", profile);
		model.addAttribute("listSkills", skillService.GetAllSkill());
		model.addAttribute("listTittles",tittleService.GetAllTittles());
		return "profileForm";
	}
}
